package com.xuecheng.basic.controller;

import com.xuecheng.basic.domain.Dictionary;
import com.xuecheng.commons.model.vo.DictionaryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.basic.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 数据字典前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class DictionaryController {

    @Autowired
    private DictionaryService  dictionaryService;

    //查询数据字典所有数据
    @GetMapping("/dictionary")
    public ResponseResult<DictionaryVo> findAll(){
        //查询
        List<Dictionary> dictionaryList = dictionaryService.list();
        //数据库实体转为vo
        List<DictionaryVo> voList = BeanHelper.copyWithCollection(dictionaryList, DictionaryVo.class);
        return ResponseResult.okResult(voList);
    }

    //根据code查询数据字典
    @GetMapping("/dictionary/{code}")
    public ResponseResult<DictionaryVo> findByCode(@PathVariable("code")String code){
        //查询字典
        Dictionary dictionary = dictionaryService.lambdaQuery().eq(Dictionary::getCode, code).one();
        //数据库实体转为vo
        DictionaryVo dictionaryVo = BeanHelper.copyProperties(dictionary, DictionaryVo.class);
        return ResponseResult.okResult(dictionaryVo);
    }
}
